package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.nbcb.ebank.NbcbB2cMessage;
/**
 * @NbcbB2cServerRes.java
 * @author LiLiLi
 */
@Controller
@Scope("prototype")
public class NbcbB2cServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(NbcbB2cServerRes.class);

	@RequestMapping(value = "/NbcbB2cServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- NbcbB2cServerRes bankback ---------------");
		return super.execute(request, response);      
	}
    
	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		NbcbB2cMessage message = new NbcbB2cMessage();
		message.setBank(BankConstants.BANK_LOGO_NBCB);
		String notifyMsg = request.getParameter("notifyMsg");
		String plainMsg = notifyMsg.substring(0,notifyMsg.lastIndexOf("|")+1)+"";
		message.setPlainMessage(plainMsg);
		String[] terms = new String[225];
		terms = notifyMsg.split("\\|");
		message.setOrderId(terms[5]);
		message.setBankOrderId(terms[5]);
		message.setOrderAmount(TransUtils.amountConvert2Yuan(terms[7]));
		message.setAmount(new BigDecimal(message.getOrderAmount()));
		message.setTranResult(terms[10]);
		message.setSignMessage(terms[terms.length-1]);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		NbcbB2cMessage message = (NbcbB2cMessage)result.getModel("res");
		OrderInfo orderInfo = new OrderInfo();
		String retCode = message.getTranResult();
		if(retCode!=null && retCode.equals("0")){		
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getOrderAmount());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		}else{
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getOrderAmount());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg("");
		}
		orderInfo.setBankName("nbcb");
		return orderInfo;
	}
	
	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		String notifyMsg = request.getParameter("notifyMsg");
		//返回的参数不存在，直接返回错误页面。
		if( StringUtils.isEmpty(notifyMsg)){
			return false;
		}
		return true;
	}
}
